/**
* @Title: selfFeedback_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年11月13日 下午10:20:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('SelfFeedbackCtrl', ['$scope','$state','$modal','BaseURL','Modal','SelfFeedbackService',
	                                function($scope,$state,$modal,baseUrl,Modal,SelfFeedbackService){
		$scope.selfFeedbackService = SelfFeedbackService;
		SelfFeedbackService.getfeedbackstatus();
		SelfFeedbackService.getfeedbackprocess();
		$scope.form = {};
		$scope.form.feedbackEndDate = {};
		
		var  cellTemplate =  '<a ng-click="clickLink(data);" ng-bind="data.tranName" ></a>'; 
		$scope.columns = [
		                    {label:'预警编号',map:'warnNoBiz',width:'12%'},
					        {label:'事件名称',map:'summary',width:'14%'},
//					        {label:'医疗机构',map:'medicalOrgName',width:'10%'},
					        {label:'处理方式',map:'process',width:'10%'},
					        {label:'预警时段',map:'happenDate',width:'14%'},
					        {label:'反馈截止日期',map:'feedbackEndDate',width:'10%'},
					        {label:'提交时间',map:'agentDate',width:'10%'},
					        {label:'审核时间',map:'auditDate',width:'10%'},
					        {label:'状态',map:'isFeedbackName',width:'10%'},
					        {label:'操作',width:'10%',cellClass:'text-center', cellTemplate:cellTemplate}
						 ];
		//分页配置
		$scope.current = {
				totalCount:0,
				currentPage:0,
				limit:20
		};
		
		//查询
		$scope.onSubmit = function() {
			$scope.current.currentPage = 1;
			query();
		}
		
		function query() {
			//参数
			var feedbackEndDateStart = null;			
			if ($scope.form.feedbackEndDate
				&& $scope.form.feedbackEndDate.start) {
				feedbackEndDateStart = $scope.form.feedbackEndDate.start;
			}
			var feedbackEndDateEnd = null;
			if($scope.form.feedbackEndDate  
				&&$scope.form.feedbackEndDate.end ){
				feedbackEndDateEnd = $scope.form.feedbackEndDate.end;
			}
			var start = 0;
			if(parseInt($scope.current.currentPage) > 1){
                  start = (parseInt($scope.current.currentPage) - 1) * $scope.current.limit;
            }
			var params = {
					warnNoBiz:$scope.form.warnNoBiz,
					summary:$scope.form.summary,					
					feedbackEndDateStart:feedbackEndDateStart,
					feedbackEndDateEnd:feedbackEndDateEnd,
					isFeedback:$scope.form.codeNo,
					process:$scope.form.process,
					start:start,
					limit:$scope.current.limit
			}
			//获取医院自查列表信息
			SelfFeedbackService.getSelfFeedback(params).success(function(res){
				 	$scope.tableData = res.result;
	                $scope.current.totalCount = res.total;
	                angular.forEach($scope.tableData,function(data,index){
	                	if(data.process=='预警提醒'){//预警提醒
	                		data.router = "selfFeedback.selfFeedbackDetail";
	                		//data.tranName = "提醒确认";
	                		if (data.isFeedbackCode == '01') {		                		
		                		data.tranName = "反馈";
		                	}
		                	else {		                		
		                		data.tranName = "查看";
		                	}
	                	}else{
	                		if (data.isFeedbackCode == '01') {
		                		data.router = "selfFeedback.selfFeedbackEdit";
		                		data.tranName = "反馈";
		                	}
		                	else {
		                		data.router = "selfFeedback.selfFeedbackDetail";
		                		data.tranName = "查看";
		                	}
	                	}
	                	if(data.actNo=='N2014009'&&(data.processNo=='02'||data.processNo=='04')){
	                		data.tranName = "";
	                	}
	                });
			});
		}
		
		//分页监听
        $scope.$on('pageTool.skipPage',function(e,v) {
        	$scope.current.currentPage = v;
        	query();
        });
        //预警提醒弹出框
        function showWarnModal ($modal,baseUrl,params){
			 var modalInstance = $modal.open({
			      templateUrl:baseUrl +'views/hcpms/warning/self/selfFeedback_warn_view.html',
			      backdrop:'static',
				  size:'sm',
				  controller: function($scope, $modalInstance,Modal,SelfFeedbackService){
					  SelfFeedbackService.getSelfFeedbackWarn(params).success(function(res){
						  $scope.warninfo={};
						  $scope.warninfo=res.warninfo;
						  $scope.warningDate=SelfFeedbackService.setPriodDate(res.warninfo.startDate,res.warninfo.endDate);

					  });
					  //关闭弹窗
					  $scope.onClose = function() {
						  $modalInstance.dismiss();
					  }	
					  //确认
					  $scope.onConfirm=function() {
						SelfFeedbackService.getSelfFeedBackWarnCheck(params).success(function(res){							 
							Modal.alert(res.msg); 
							if(res.result==true){							 
							  $modalInstance.close(true);
							  }
						    });
						 
					  }	
				  }
			 });
			 modalInstance.result.then(function(isFlat) {		 			
			 		if(isFlat){
			 			 angular.forEach($scope.tableData,function(data,index){
			 				 if(params.warnNo === data.warnNo){
			 					data.isFeedbackName = '已确认';
			 					data.tranName = '查看';
			 					data.router = "selfFeedback.selfFeedbackDetail";
			 				 }
						 });
			 		}
		  	    });
       }
        //页面跳转
        $scope.clickLink = function(data) {
        	if(data.process=='提醒'){        		
        		showWarnModal($modal,baseUrl,{warnNo:data.warnNo});
        	}else{
        		$state.go(data.router,{warnNo:data.warnNo});
        	}        	
        }
       
		 //重置
        $scope.resetBut = function() {
			$scope.form = {};
			$scope.form.feedbackEndDate = {};
		}
        
        $scope.onSubmit();
	}]);
})();